var app = getApp()
var util = require('../../utils/util.js')
var api = require('../../config/api.js');
var user = require('../../utils/user.js');

Page({
  data: {
    focus: false,
    inputValue: '',
    isable: true,
    isdoublable: false,
    name: '',
    cardNo: '',
    mobile: '',
    code: '',
    xyht: false,
    codeFlag:false
  },
  onLoad: function (option) {
    var userid = wx.getStorageSync("userid");
    if (userid == '') {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    var that = this;
    util.request(api.queryUserByWxidUrl, {
      id: userid,
      key:api.key
    }).then(function (res) {
      if(res.errno === 402){
        wx.showModal({
          title: '错误',
          content:'不被允许的请求',
          showCancel: false
        })
        return;
      }
      if (parseInt(res.data.countNum) > 0) {
        that.setData({
          isable: false
        });
      }
      if (parseInt(res.data.dCardNo) > 0) {
        that.setData({
          isdoublable: true
        });
      }
    })

  },
  checkboxChange: function (e) {
    //console.log(JSON.stringify(e.detail.value[0]));
    if (typeof (e.detail.value[0]) != 'undefined') {
      this.setData({
        xyht: true
      })
    }
  },
  onReady: function (option) {
    //console.log('vvvvvvvvvvvvvvvvv');
    var userid = wx.getStorageSync("userid");
    var that = this;
    util.request(api.bdingPcUserUrl, {
      id: userid
    }).then(function (res) {
      /*wx.showModal({
        title: '提示',
        content: '绑定成功！',
        showCancel: false
      })*/
    })

  },
  bindname: function (e) {
    this.setData({
      name: e.detail.value.replace(/\s+/g, '')
    })
  },
  bindcardNo: function (e) {
    this.setData({
      cardNo: e.detail.value.replace(/\s+/g, '')
    })
  },
  bindnmobile: function (e) {
    this.setData({
      mobile: e.detail.value.replace(/\s+/g, '')
    })
  },
  //保存验证码
  bindCode: function (e) {
    this.setData({
      code: e.detail.value.replace(/\s+/g, '')
    })
  },
  getCode() {
    var that = this
    console.log()
    if (this.data.mobile == '') {
      wx.showModal({
        title: '提示',
        content: '请填写联系电话！',
        showCancel: false
      })
      return;
    }
    if (this.data.mobile.length < 11) {
      wx.showModal({
        title: '提示',
        content: '请检查手机号码是否正确！',
        showCancel: false
      })
      return;
    }
    //进行发送验证码
    setTimeout(function () {
      that.openNewCode()
    }, 30 * 1000)
    this.setData({
      codeFlag: true
    })
    util.request(api.getCodeUrl, {
      mobile: this.data.mobile
    }).then(function (res) {
      wx.showToast({
        title: '短信验证码已经发送成功！',
        icon: 'none'
      })
    })
  },
  bindPcUser() {
    var userid = wx.getStorageSync("userid");
    var that = this;
    util.request(api.bdingPcUserUrl, {
      id: userid
    }).then(function (res) {
      that.setData({
        isdoublable: false
      });
      wx.showModal({
        title: '提示',
        content: '绑定成功！',
        showCancel: false
      })
    })
  },
  openNewCode(){
    this.setData({
      codeFlag: false
    })
  },
  bindUser() {
    if (this.data.name == '') {
      wx.showModal({
        title: '提示',
        content: '请填写姓名！',
        showCancel: false
      })
      return;
    }
    if (this.data.cardNo == '') {
      wx.showModal({
        title: '提示',
        content: '请填写证件号码！',
        showCancel: false
      })
      return;
    }
    if (this.data.mobile == '') {
      wx.showModal({
        title: '提示',
        content: '请填写联系电话！',
        showCancel: false
      })
      return;
    }
    // if (this.data.code == '') {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请填写验证码！',
    //     showCancel: false
    //   })
    //   return;
    // }

    if (!this.data.xyht) {
      wx.showModal({
        title: '提示',
        content: '请先认真阅读并同意协议！',
        showCancel: false
      })
      return;
    }
    var userid = wx.getStorageSync("userid");
    var that = this;
    util.request(api.bindUserUrl, {
      id: userid,
      name: that.data.name,
      cardNo: that.data.cardNo,
      mobile: that.data.mobile,
      //取消检验验证码
      // code: that.data.code
    }).then(function (res) {
      console.log(res)
      if (res.data != '1') {
        wx.showModal({
          title: '提示',
          content: res.data,
          showCancel: false
        })
      } 
      if(res.errno == '404') {
        wx.showModal({
          title: '验证码错误',
          content: res.errmsg,
          showCancel: false
        })
      } else {
        wx.showModal({
          title: '绑定成功',
          content: '当前学员的信息绑定成功！',
          showCancel: false,
          success: function () {
              wx.switchTab({
                url: '/pages/user/user',
              })
          },
        })
      }
    })
  }
})